package com.zjbbkj.baibai.netty.message.receive;

import com.alibaba.fastjson.TypeReference;
import com.sun.xml.internal.bind.v2.TODO;
import com.zjbbkj.baibai.common.utils.wanli.JSONUtils;
import com.zjbbkj.baibai.common.utils.wanli.RedisCacheUtil;
import com.zjbbkj.baibai.domain.device.Device;
import com.zjbbkj.baibai.netty.feign.DeviceFeignService;
import com.zjbbkj.baibai.netty.feign.OrderFeignService;
import com.zjbbkj.baibai.netty.maps.ContainerMaps;
import com.zjbbkj.baibai.netty.message.factory.Operation;
import com.zjbbkj.baibai.netty.pojo.ProtocolMessage;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.zjbbkj.baibai.netty.message.factory.OperatorFactory.write;
import static com.zjbbkj.baibai.netty.message.sendpackage.ASY.ASY_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.OTC.OTC_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.STB.STB_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.USB.USB_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.USC.USC_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.UWA.UWA_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.UWC.UWC_DEVICE;

/**
 * @author WanLi
 * @date 2021年08月24日 17:34
 */


@Slf4j
public class ReceiveRP implements Operation {


    @Resource
    private RedisCacheUtil redisCacheUtil;

    @Resource
    private DeviceFeignService deviceFeignService;

    @Resource
    private OrderFeignService orderFeignService;



    @Override
    public void parsingMsg(String message, Channel channel) {
        String type = message.substring(3, 6);  //指令類型
        try {
            if (type.equals("ASY")) {
                ASY_HANDLER(message, channel);
            } else if (type.equals("UWC")) {
                UWC_HANDLER(message, channel);
            } else if (type.equals("UWA")) {
                UWA_HANDLER(message, channel);
            } else if (type.equals("USB")) {
                USB_HANDLER(message, channel);
            } else if (type.equals("USC")) {
                USC_HANDLER(message, channel);
            } else if (type.equals("OTC")) {
                OTC_HANDLER(message, channel);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    private static void OTC_HANDLER(String message, Channel channel) throws Exception {
            write(OTC_DEVICE(), channel);
    }

    public static void USB_HANDLER(String message, Channel channel) {
        try {
            write(USB_DEVICE("1"), channel);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    /**
     * 屏幕板卡通讯异常报警上报
     *
     * @param message
     * @param channel
     */
    public void USC_HANDLER(String message, Channel channel) {
        try {
            write(USC_DEVICE("1"), channel);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    /**
     * 设备能力级上报
     * @Description //_RPASY00000001312#/#200#/#10
     * @param message  指令
     * @param channel  通道
     */
    public void ASY_HANDLER(String message, Channel channel) {
        try {
            write(ASY_DEVICE("1"), channel);
            //设备能力级解析数据
            Map<Channel, String> channelsX = ContainerMaps.getChannelsX();
            if (!channelsX.containsKey(channel)) {
                return;
            }
            String IMEIID = channelsX.get(channel);
            String[] split = message.split("#/#");
            String deviceMaxNum = split[0].substring(15);
            ProtocolMessage p = (ProtocolMessage)redisCacheUtil.get(IMEIID);
            Device device = deviceFeignService.getDevice(IMEIID).getData("device",new TypeReference<Device>(){});
            if (deviceMaxNum.equals("12")) {
                device.setDeviceType("3");
                p.setDeviceStatus("3");
            } else {
                device.setDeviceType("1");
                p.setDeviceStatus("1");
            }
            deviceFeignService.updateDevice(device);
            if (split.length == 4) {
                p.setFontType(split[3]);
            }
            redisCacheUtil.set(IMEIID,p);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    /**
     * 充电完成上报
     * @Description 返回示例
     *             //_RPUWCB10002008:[0]#/#0   大蜡烛返回数据
     *             //_RPUWCB10002022777777:[0,1,2,3]#/#0  12PIN返回数据
     *             //_RPUWCB10002034226586:[0,1,2,3,4,5,6,7,8,9,10,11]   12PIN返回数据
     * @param message
     * @param channel
     */
    public void UWC_HANDLER(String message, Channel channel) {
        try {
            //
            //充电完成上报
            write(UWC_DEVICE("1"), channel);
            //更新端口状态
            write(STB_DEVICE("ALL"), channel);

            String IMEIID = ContainerMaps.getChannelsX().get(channel);
            Device device = deviceFeignService.getDevice(IMEIID).getData("device",new TypeReference<Device>(){});
            String json = message.substring(15);
            //获取设备订单号
            String[] split = json.split(":");
            String orderNum = split[0];
            String portsStr = split[1];
            String[] ports = portsStr.split("#/#")[0].replaceAll("[\\[\\]]", "").split(",");//端口号
            ProtocolMessage p = (ProtocolMessage)redisCacheUtil.get(IMEIID);
            //获取STB
            Map<Object, Object> map = p.getDeviceStb();
            if (!p.getDeviceStb().isEmpty()) {
                for (String port : ports) {
                    map.put(port,"0");
                }
                p.setDeviceStb(map);
                redisCacheUtil.set(IMEIID,p);
            }
            //TODO  更新预约订单
//            //订单更新
//            Orders orderById = null;
//            if(device.getDeviceType().equals("1")){
//                orderById = ordersService.getOrderByNum(stringObjectMap.get("orderNum").toString(),device.getDeviceImei());
//            }else if(device.getDeviceType().equals("3")){
//                orderById = ordersService.getOrderByNum(orderNum,device.getDeviceImei());
//            }
//            //更新订单状态
//            if (!MyUtils.isNull(orderById)) {
//                orderById.setOrderCurrentStatus("2");
//                ordersService.update(orderById);
//                Appointment appointment = appointmentService.getAppointmentByOrderId(orderById.getOrderId());
//                //更新预约订单状态
//                if (!MyUtils.isNull(appointment)) {
//                    appointment.setAppointmentStatus("0");
//                    appointmentService.update(appointment);
//                }
//            }
//            log.info("设备" + IMEIID + "充电完成上报");
//            appointmentService.appointmentProcessing(IMEIID);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }


    /**
     * 刷卡上报
     *
     * @param message
     * @param channel
     */
    public static void UWA_HANDLER(String message, Channel channel) {
        try {
            //刷卡上报
            String IMEIID = ContainerMaps.getChannelsX().get(channel);
            log.info("设备" + IMEIID + "刷卡上报");
            write(UWA_DEVICE("1"), channel);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }


}
